﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Timers;
using System.Threading;
using System.IO;

public class Task
{
    System.Timers.Timer timer = null;

    public bool IsRunning { get; set; }
    public DateTime LastRunTime { get; set; }
    public bool IsLastRunSuccessful { get; set; }
    public double Interval { get; set; }
    public bool Stopped { get; set; }    

    public Task(double interval)
    {
        this.Interval = interval;
        Initialize();
    }

    public void Start()
    {
        this.Stopped = false;
        this.StartTask();
    }

    public void Stop()
    {
        this.Stopped = true;
    }

    private void Initialize()
    {
        this.Stopped = false;
        timer = new System.Timers.Timer(this.Interval);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;
    }

    private void StartTask()
    {
        if (!this.Stopped)
        {
            Thread thread = new Thread(new ThreadStart(Execute));
            thread.Start();
        }
    }

    private void Execute()
    {
        try
        {
            this.IsRunning = true;
            this.LastRunTime = DateTime.Now;

            LinQ_RaoVatDataContext db = new LinQ_RaoVatDataContext();            
            var qstrAuToUp = from p in db.AUTO_UPs where p.DaUp == false select p;
            if(qstrAuToUp.Any() == false)
                return;
            else
            {
                foreach(AUTO_UP autoup in qstrAuToUp)
                {
                    DateTime now = DateTime.Now;
                    DateTime timUp = (DateTime)autoup.ThoiGianUp;
                    TimeSpan tp = timUp.Subtract(now);                    
                    if (tp.TotalSeconds < 60)
                    {
                        TIN_RAO_VAT tinRaoVat = db.TIN_RAO_VATs.Single(p => p.MaTinRaoVat == autoup.MaTinRaoVat);
                        tinRaoVat.ThoiGianDang = DateTime.Now;
                        autoup.DaUp = true;
                        NGUOI_DUNG nguoiDung = db.NGUOI_DUNGs.Single(q => q.MaNguoiDung == tinRaoVat.MaNguoiDung);
                        if (nguoiDung.DiemVIP < 5)
                        {

                        }
                        else
                        {  
                           

                            TIN_NHAN tn = new TIN_NHAN();

                            tn.Deleted = false;
                            tn.MaNguoiGoi = 8;
                            tn.MaNguoiNhan = nguoiDung.MaNguoiDung;
                            tn.Readed = false;
                            tn.TieuDeTinNhan = "Thông báo Auto Up";
                            tn.ThoiGianNhanTin = now;
                            string tenTinRaoVat ="";
                            
                             var query =  db.TIN_RAO_VATs.Where(t => t.MaTinRaoVat == autoup.MaTinRaoVat);

                            if (query.Count() == 0)
                            {
                            }
                            else
                            {
                                tenTinRaoVat = query.First().TenTinRaoVat;
                                  nguoiDung.DiemVIP -= 5;
                            tn.NoiDungTinNhan = string.Format("Bài rao vặt tên là:{0}, đã được auto up vào lúc {1}, số điểm VIP trong tài khoản của bạn bị trừ 5 VIP, còn lại là {2}",tenTinRaoVat, now.ToShortDateString() + " " + now.ToShortTimeString(), nguoiDung.DiemVIP) ;

                            db.TIN_NHANs.InsertOnSubmit(tn);

                            db.SubmitChanges();

                            }
                           

                           
                        }
                    }
                }
            }
            this.IsLastRunSuccessful = true;
        }
        catch
        {
            this.IsLastRunSuccessful = false;
            // Handle the exception
        }
        finally
        {
            this.IsRunning = false;
        }
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (!this.IsRunning)
            StartTask();
    }
}
